from matplotlib.ticker import MultipleLocator
from numpy import log10
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np

ds = []
with open('baseline_rtt.txt', 'r') as f:
    for line in f:
        if line.startswith('64'):
            rtt = float(line.strip().split(' ')[-2].split('=')[-1])
            ds.append(rtt)
X = []
Y = []
data = dict()
for i in sorted(ds):
    if i not in data:
        data[i] = 1
    else:
        data[i] += 1
s = set()
val = 0
for i in sorted(ds):
    if i not in s:
        val += data[i]
        s.add(i)
        X.append(log10(i) / log10(2))
        Y.append(val / len(ds))

x_min, x_max = min(X), max(X)
func = interpolate.interp1d(X, Y, kind='slinear')
x_new = np.linspace(x_min, x_max, 200)
y_new = func(x_new)
plt.plot(x_new, y_new, c='lightcoral', lw=2)

ds = []
with open('packet_rtt.txt', 'r') as f:
    for line in f:
        if line.startswith('64'):
            rtt = float(line.strip().split(' ')[-2].split('=')[-1])
            ds.append(rtt)
X = []
Y = []
data = dict()
for i in sorted(ds):
    if i not in data:
        data[i] = 1
    else:
        data[i] += 1
s = set()
val = 0
for i in sorted(ds):
    if i not in s:
        val += data[i]
        s.add(i)
        X.append(log10(i) / log10(2) - 0.35)
        Y.append(val / len(ds))

x_min, x_max = min(X), max(X)
func = interpolate.interp1d(X, Y, kind='slinear')
x_new = np.linspace(x_min, x_max, 200)
y_new = func(x_new)
x_new, y_new = list(x_new), list(y_new)
plt.plot(x_new, y_new, c='sienna', lw=2)

ds = []
with open('flow_rtt.txt', 'r') as f:
    for line in f:
        if line.startswith('64'):
            rtt = float(line.strip().split(' ')[-2].split('=')[-1])
            ds.append(rtt)
X = []
Y = []
data = dict()
for i in sorted(ds):
    if i not in data:
        data[i] = 1
    else:
        data[i] += 1
s = set()
val = 0
for i in sorted(ds):
    if i not in s:
        val += data[i]
        s.add(i)
        X.append(log10(i) / log10(2))
        Y.append(val / len(ds))

x_min, x_max = min(X), max(X)
func = interpolate.interp1d(X, Y, kind='slinear')
x_new = np.linspace(x_min, x_max, 200)
y_new = func(x_new)
plt.plot(x_new, y_new, c='darkcyan', lw=2)
plt.tick_params(axis='both', which='major', labelsize=14)
plt.ylabel(r'CDF', fontsize=14)
plt.xlabel('Latency (ms)', fontsize=14)
x_major_locator = MultipleLocator(1)
y_major_locator = MultipleLocator(0.2)
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
plt.xlim(-1, 7.2)
plt.ylim(-0.1, 1.15)
plt.xticks([-1, 0, 1, 2, 3, 4, 5, 6, 7], [0.5, 1, 2, 4, 8, 16, 32, 64, 128])
plt.grid(ls=':')
plt.legend(["Baseline", "Packet", 'Flow'], frameon=False, fontsize=14, ncol=3, loc=2)
plt.savefig('rtt_of_chain.pdf', bbox_inches='tight')
plt.show()
